// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database() //代码块为cdb
const article = db.collection('article');
module.exports = {
	_before: function () { // 通用预处理器

	},
	async add(cn_text, uy_text) {
		let time = Math.round(new Date() / 1000);
		let res = await article.add({
			'cn_text' : cn_text,
			'uy_text' : uy_text,
			"add_time": time,
			"update_time": time
		})
		return res;
	},
	async update(id, cn_text, uy_text) {
		let time = Math.round(new Date() / 1000);
		let res = await article.doc(id).update({
			'cn_text' : cn_text,
			'uy_text' : uy_text,
			"update_time": time
		})
		return res;
	},
	async getAll($page = 1, $limit = 20, cn = '', uy = '') {
		let res = await  article.orderBy("add_time", "desc")
		.skip(($page - 1) * $limit) // 跳过前20条
		.limit($limit) // 获取20条
		.where({
			cn_text : new RegExp(cn,'ig'),
			uy_text : new RegExp(uy,'ig'),
		})
		.get();
		return res.data;
	},
	
	async count(cn = '', uy = '') {
		let res = await article.where({
			cn_text : new RegExp(cn,'ig'), 
			uy_text : new RegExp(uy,'ig'),
		}).count();
		return res.total;
	},
	
	async del(id) {
		let res = await article.doc(id).remove();
		return res
	},
}
